package fr.uniscol.dao.mapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

import fr.uniscol.dao.IGeographieDao;
import fr.uniscol.model.Commune;

public class CommuneRowMapper implements RowMapper<Commune> {

	private IGeographieDao geoDao;
	
	public CommuneRowMapper(IGeographieDao geoDao){
		this.geoDao = geoDao;
	}

	@Override
	public Commune mapRow(ResultSet rs, int line) throws SQLException {
		
		Commune c = new Commune();
		c.setId(rs.getInt("ID"));
		c.setCodeInsee(rs.getString("CODE"));
		c.setLibelleLong(rs.getString("NOM"));
		
		if(rs.getString("DEPARTEMENTID") != null){
			c.setDepartement(geoDao.getDepartementById(rs.getInt("DEPARTEMENTID")));
		}
		
		return c;
	}
}
